package com.xxl.job.executor.service;

import com.xxl.job.core.log.XxlJobLogger;
import com.xxl.job.executor.kafka.KafkaProduceService;
import com.xxl.job.executor.model.*;
import com.xxl.job.executor.repository.CurrentInfoRepository;
import com.xxl.job.executor.utils.ThreadLocalDateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class FireCollectService {

    private static final Logger LOGGER = LoggerFactory.getLogger(FireCollectService.class);


    @Autowired
    private SignalDefService signalDefService;
    @Autowired
    private CurrentInfoRepository currentInfoRepository;
    @Autowired
    private KafkaProduceService kafkaProduceService;

    @Autowired
    private BkJsonDataService bkJsonDataService;

    public void save(SignalDefXfModel model, int flag){
        SignalDefXfModel signalDefXfModel=signalDefService.queryXfSignal(
                model.getGatherId(), model.getGatherResId(),model.getRegisterAddr(),model.getModuleAddr(),model.getPointAddr());
        if(signalDefXfModel==null){
            LOGGER.error("消防系统：未找到对应的信号配置");
            return;
        }

        String nDate="";
        nDate= ThreadLocalDateUtil.formatDate(new Date());
        String signalType  =signalDefXfModel.getSignalType();

        //新建实时存储的实体
        CurrentInfoXfModel currentInfoXfModel=new CurrentInfoXfModel();
        currentInfoXfModel.setValue(model.getSignalValue());
        currentInfoXfModel.setGatherResId(model.getGatherResId());
        currentInfoXfModel.setGatherId(model.getGatherId());
        currentInfoXfModel.setDevCode(signalDefXfModel.getDevCode());
        currentInfoXfModel.setSignalCode(signalDefXfModel.getCode());
        currentInfoXfModel.setSignalName(signalDefXfModel.getName());
        currentInfoXfModel.setSignalType(signalDefXfModel.getSignalType());
        currentInfoXfModel.setValunit(signalDefXfModel.getValunit());
        currentInfoXfModel.setTime(nDate);
        currentInfoXfModel.setPointAddr(signalDefXfModel.getPointAddr());

        //根据实体去redis查询上次信号时间
        CurrentInfoXfModel currentInfoXfModel1=currentInfoRepository.queryXfCurrent(signalDefXfModel.getDevCode(),signalDefXfModel.getCode());

        if(currentInfoXfModel1==null){
            currentInfoXfModel.setLastTime(nDate);
        }else{
            currentInfoXfModel.setLastTime(currentInfoXfModel1.getTime());
        }
        currentInfoXfModel.setSendTime(ThreadLocalDateUtil.formatDate(new Date()));
        boolean isSend=this.isSaveRedis(currentInfoXfModel,currentInfoXfModel1,signalType);
        if(isSend){
            //实时数据存入redis
            currentInfoRepository.save(currentInfoXfModel);

            //发送kafka
            send(currentInfoXfModel);


            //发送博坤
            bkJsonDataService.sendDevInfoMessage(signalDefXfModel.getCode(),
                    signalDefXfModel.getName(),
                    signalDefXfModel.getSignalType(),
                    signalDefXfModel.getDevCode(),
                    model.getSignalValue(),
                    signalDefXfModel.getValunit(),
                    nDate);
        }

    }

    /**
     * 发kafka
     * @param message
     */
    public void send(CurrentInfoXfModel message) {

        try {
            this.kafkaProduceService.sendXfAlarmMessage(message);

        } catch (Exception e) {
            XxlJobLogger.log("发送告警信息失败:"+message.toString(), e);
        }
    }
        /**
         * 判断实时数据是否需要存入redis
         * @param automaticControlStatus
         * @param automaticControlStatus1
         * @return
         */
        private boolean isSaveRedis(CurrentInfoXfModel automaticControlStatus,
                CurrentInfoXfModel automaticControlStatus1, String signalType){
            int fa = 0;
            long iTimeDiffHour=0;
            String autoVal=automaticControlStatus.getValue();
            String oldautoVal=automaticControlStatus1.getValue();
            if(automaticControlStatus1 != null){
                if(!autoVal.equals(oldautoVal)){
                    fa = 1;
                }
                iTimeDiffHour = (ThreadLocalDateUtil.parse(automaticControlStatus.getTime()).getTime() - ThreadLocalDateUtil.parse(automaticControlStatus1.getTime()).getTime()) / (1000 * 60* 20);
            }else{
                iTimeDiffHour = 2;
            }

            if ( fa > 0 || iTimeDiffHour > 1) {
                return true;
            }
            return false;
        }


    }
